
Object.prototype.clone =function()
{
	 var newObj = (this instanceof Array) ? [] : {};
	  for (i in this) 
	  {
			if (i == 'clone') continue;
				if (this[i] && typeof this[i] == "object") {
			newObj[i] = this[i].clone();
		} else newObj[i] = this[i]
	  } return newObj;
}

/*!	brief The mouse state object will correspond to a static enum storing state the mouse can have	*/
 MouseState = function()
{

}

MouseState.Down		=0;
MouseState.Up		=1;
MouseState.Pressed	=2;
MouseState.Nothing	=3;

var MouseEventType = function()
{

}

MouseEventType.Down			=	0;
MouseEventType.Up			=	1;
MouseEventType.Move		=	2;

var Mouse 		=	function()
{

}


 MouveEventArg = function(type, button, x, y)
{
	this.m_Type;		/*!< Store the type of mouse event : 0 -> down 1 -> up 2 -> move	*/
	this.m_Button;		/*!< Button Used	*/
	
	this.PositionX;
	this.PositionY;
	
	this.m_Type 		= 	type;
	this.m_Button		=	button;
	
	this.PositionX		= 	x;
	this.PositionY		= 	y;
	
}

/********************************/
/*			Static				*/
/********************************/

	/********************************/
	/*			Properties			*/
	/********************************/

		Mouse.LastPositionX;	/*!< Last position stored by the mouse in X when MouseMove call	*/
		Mouse.LastPositionY;		
		
		Mouse.PositionX;		/*!< Actual mouse position in X 	*/
		Mouse.PositionY;
		
		Mouse.MouseMove;		/*!< Did the mouse move this frame?	*/
		Mouse.MouseDrag;		/*!< Is the user dragging something? (mouse button pressed with mouse move event	*/
		Mouse.MousePressed;
		
		Mouse.Buttons = new Array(MouseState.Nothing, MouseState.Nothing, MouseState.Nothing);
		
		Mouse.MouseMoveHandlers		=	new Array();
		Mouse.MouseDragHandlers		=	new Array();
		
		Mouse.MouseDownHandlers		=	new Array();
		Mouse.MouseUpHandlers		=	new Array();
		Mouse.MousePressedHandlers	=	new Array();

		
		Mouse.MouseEvents				=	new Array();	/*! Store all mouse events wich occured between 2 draw call	*/
		
	/********************************/
	/*			Methods 			*/
	/********************************/
	
		Mouse.Init	=	function()
		{
			Application.Canvas.addEventListener("mousemove", 	Mouse.OnMouseMove	, false);
			Application.Canvas.addEventListener("mouseup", 		Mouse.OnMouseUp		, false);
			Application.Canvas.addEventListener("mousedown", 	Mouse.OnMouseDown	, false);
			
			Application.Canvas.oncontextmenu = function(){return false;}		//disable right click contextual menu
		}
		
		Mouse.Nothing = function(){return false;};
		Mouse.CheckEvents=	function()
		{
			Mouse.MouseMove		=	false;
			Mouse.MouseDrag		=	false;
			Mouse.MousePressed	=	false;
			
			//First, I must check if a mouse button is at "up" state to put it in "nothing" state	

			for(var i =0; i<3; i++)
			{
				if(Mouse.Buttons[i]==MouseState.Up)
					Mouse.Buttons[i]=MouseState.Nothing;
			}
			//Then, I must check if a mouse button is at "down" state, to put it in "pressed" state
			for(var i =0; i<3; i++)
			{
				if(Mouse.Buttons[i]==MouseState.Down)
					Mouse.Buttons[i]=MouseState.Pressed;
			}
			
			while( Mouse.MouseEvents.length!=0)
			{
				var event = Mouse.MouseEvents.shift();
				
				if(event.m_Type==MouseEventType.Move)
				{
					Mouse.LastPositionX		= 	Mouse.PositionX ;
					Mouse.LastPositionY 	= 	Mouse.PositionY;
					
					Mouse.PositionX			= 	event.PositionX - Application.Canvas.offsetLeft;
					Mouse.PositionY			= 	event.PositionY	- Application.Canvas.offsetTop;
					
					Mouse.MouseMove=true;
				}
				else if(event.m_Type==MouseEventType.Down)
				{
					Mouse.Buttons[event.m_Button]=MouseState.Down;
				}
				else if(event.m_Type	== MouseEventType.Up)
				{
					Mouse.Buttons[event.m_Button]=MouseState.Up;
				}
			}
		}
		
		Mouse.Update	=	function()
		{		
			Mouse.CheckEvents();
			
			for( var i = 0; i<Mouse.Buttons.length; i++)
			{
				if(Mouse.Buttons[i]==MouseState.Down)
				{
					for (var j =0; j<Mouse.MouseDownHandlers.length; j++)
					{
						Mouse.MouseDownHandlers[j].Fire(Mouse.PositionX,Mouse.PositionY);
					}
				}
				if(Mouse.Buttons[i]==MouseState.Up)
				{
						for (var j =0; j<Mouse.MouseUpHandlers.length; j++)
						{
							Mouse.MouseUpHandlers[j].Fire(Mouse.PositionX, Mouse.PositionY);
						}
				}
				if(Mouse.Buttons[i]==MouseState.Pressed)
				{
					Mouse.MousePressed	=	true;
					
					for (var j =0; j< Mouse.MousePressedHandlers.length; j++)
					{
						MousePressedHandlers[j].Fire(Mouse.PositionX, Mouse.PositionY);
					}
				}
			}
			
			if(Mouse.MouseMove==true)
			{
				for (var i =0; i< Mouse.MouseMoveHandlers.length; i++)
				{
					Mouse.MouseMoveHandlers[i].Fire(Mouse.PositionX, Mouse.PositionY);
				}

				if(Mouse.MousePressed==true)
				{
					for (var i =0; i< Mouse.MouseDragHandlers.length; i++)
					{
						Mouse.MouseDragHandlers[i].Fire(Mouse.PositionX - Mouse.LastPositionX, Mouse.PositionY- Mouse.LastPositionY);
					}
				}
			}
		}
	
		
		Mouse.OnMouseMove	=	function(event)
		{
			Mouse.MouseEvents.push(new MouveEventArg(MouseEventType.Move, 0, event.x, event.y));
		}
		
		Mouse.OnMouseUp	=	function(event)
		{
			Mouse.MouseEvents.push(new MouveEventArg(MouseEventType.Up, event.button));
		}
		
		Mouse.OnMouseDown	=	function(event)
		{

			Mouse.MouseEvents.push(new MouveEventArg(MouseEventType.Down, event.button));
		}

